package com.example.day09_zuoye.frag;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import androidx.fragment.app.Fragment;

import com.example.day09_zuoye.MainActivity;
import com.example.day09_zuoye.R;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class XiaZai_Fragment extends Fragment implements View.OnClickListener {
    String url = "http://cdn.banmi.com/banmiapp/apk/banmi_330.apk";
    private View view;
    private TextView xiazai;
    private Button xiazaibutt;
    private ProgressBar pro;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.xiazai_layout, null);
        initView(view);
        return view;
    }

    private void initView(View view) {
        xiazai = view.findViewById(R.id.xiazai_text);
        xiazaibutt = view.findViewById(R.id.xiazai_butt);
        pro = view.findViewById(R.id.progress);
        xiazaibutt.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        //下载
        switch (view.getId()){
            case R.id.xiazai_butt:
                download();
                break;
        }
    }

    private void download() {
        Request build = new Request.Builder()
                .url(url)
                .get()
                .build();
        OkHttpClient okHttpClient = new OkHttpClient();
        okHttpClient.newCall(build).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                //ok下载
                long maxSize = response.body().contentLength();
                pro.setMax((int) maxSize);
                InputStream inputStream = response.body().byteStream();
                String s = Environment.getExternalStorageDirectory() + File.separator + "banmi_330.apk";
                File file = new File(s);
                if (!file.exists()) {
                    try {
                        file.createNewFile();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                FileOutputStream outputStream = new FileOutputStream(file);

                byte[] bytes = new byte[1024 * 2];
                //每次读取的长度
                int leng = 0;
                int count = 0;
                while ((leng = inputStream.read(bytes)) != -1) {
                    outputStream.write(bytes, 0, leng);
                    count += leng;
                    pro.setProgress(count);
            }

                outputStream.close();
                inputStream.close();
            }
        });
    }
}